<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="bug" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hShowAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list" default-expand-all>
          <template v-slot="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px ; width: 100%">
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hShowAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hShowEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length === 0 " @click.native="hDel(data.id)">删除部门</el-dropdown-item>

                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>

      </el-card>
    </div>

    <el-dialog
      :title="isEdit ? '编辑部门' : '添加子部门'"
      :visible.sync="shoeDialog"
      width="50%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <add-or-edit v-if="shoeDialog" :id="curId" :origin-list="originList" :is-edit="isEdit" @success="hSuccess" @close="hCancelb" />
    </el-dialog>

    <!-- <el-dialog
      title="编辑或添加"
      :visible.sync="shoeDialogEdit"
      width="50%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <add-or-edit v-if="shoeDialogEdit" :id="curId" :is-edit="isEdit" @success="hSuccess" />
    </el-dialog> -->

  </div>
</template>
<!-- 放置一个el-tree组件 -->

<script>
import { getDepartment, delDepartment } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import AddOrEdit from './deptDialog.vue'
export default {
  components: {
    AddOrEdit
  },
  data() {
    return {
      // 依赖一份树形数据
      // 依赖一份树形数据
      // list: [{
      //   name: '财务部',
      //   manager: '刘备',
      //   children: [
      //     {
      //       name: '财务核算部',
      //       manager: '张飞'
      //     },
      //     {
      //       name: '税务核算部',
      //       manager: '关羽'
      //     }
      //   ]
      // }]
      list: [],
      shoeDialog: false,
      curId: '',
      isEdit: false,
      // shoeDialogEdit: false
      originList: []
    }
  },
  created() {
    this.loadDepartment()
  },
  methods: {
    async loadDepartment() {
      try {
        const res = await getDepartment()
        // 使用 shift 取出第一条数据
        res.data.depts.shift()
        // console.log(res.data.depts)
        // this.originList = res.data.depts.map(item => {
        //   return { id: item.id, pid: item.pid, name: item.name, code: item.code }
        // })
        // this.originList = res.data.depts.map(item => ({ id: item.id, pid: item.pid, name: item.name, code: item.code }))
        this.originList = res.data.depts.map(({ id, pid, name, code }) => ({ id, pid, name, code }))

        console.log(this.originList)

        this.list = tranListToTreeData(res.data.depts)
      } catch (e) {
        console.log(e)
      }
    },
    hShowAdd(id) {
      // 显示添加的 dailog
      this.shoeDialog = true
      // 获取当前被点击的部门 id  传给子组件
      this.curId = id
      // 将状态修改为添加
      this.isEdit = false
    },
    hSuccess() {
      // 关闭 dialog
      this.shoeDialog = false
      // this.shoeDialogEdit = false
      // 获取最新的部门信息
      this.loadDepartment()
    },
    hShowEdit(id) {
      this.shoeDialog = true
      // 获取当前被点击的部门 id  传给子组件
      this.curId = id
      // 传递一个表示给子组件 ,现在点击的是编辑状态
      this.isEdit = true
    },
    // 用户点击删除
    hDel(id) {
      // 弹窗确认是否真的要删除
      this.$confirm('真的要删除吗?嘤嘤嘤!!!', '提示', { type: 'warning' })
        .then(async() => {
          try {
            // 发请求
            const res = await delDepartment(id)
            console.log(res)
            // 提醒用户删除成功
            this.$message.success(res.message)
            // 删除成功后重新渲染
            this.loadDepartment()
          } catch (e) {
            // 提醒用户删除失败
            this.$message.error(e.message)
          }
        })
        .catch(() => {})
    },
    hCancelb() {
      // 父组件接收子组件抛出的事件 点击取消关闭 dailog
      this.shoeDialog = false
      this.isEdit = false
    }
  }
}
</script>
